package org.gthelper.imageProcess;

import java.awt.Graphics2D;
import java.awt.Image;
//import java.awt.Point;
import java.awt.image.BufferedImage;
//import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.WritableRaster;

import org.geotools.coverage.GridSampleDimension;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageFactory;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.renderer.lite.gridcoverage2d.RasterSymbolizerHelper;
import org.geotools.renderer.lite.gridcoverage2d.SubchainStyleVisitorCoverageProcessingAdapter;
import org.geotools.styling.ChannelSelection;
import org.geotools.styling.ContrastEnhancement;
import org.geotools.styling.RasterSymbolizer;
import org.geotools.styling.SelectedChannelType;
import org.geotools.styling.StyleFactory;
import org.opengis.filter.FilterFactory2;
import org.opengis.geometry.Envelope;
import org.opengis.style.ContrastMethod;


/**
 * 获取png格式缩略图
 * @author 陈宁
 * 		
 */
public class PreviewGeter {
	
	/**
	 * 输入一张renderedimage类型图片，返回其对应的一张png图片
	 * 对于单个波段图象和多个波段的图象都使用彩色渲染
	 */
	public BufferedImage transformtopng(RenderedImage sourceimage,int ImageWidth,int ImageHeight)
	{
		RenderedImage mediaImage = null;
		if(sourceimage.getSampleModel().getNumBands()<3) 
		{
			GeneralEnvelope env = new GeneralEnvelope(DefaultGeographicCRS.WGS84);	
		    env.setRange(0, 0, 360);
		    env.setRange(1, -90, 90);		
			GridCoverage2D newCoverage2d =  creategeotifffromrenderimage(sourceimage, env, null);
			final RasterSymbolizer rs_1 = createGreyscaleStyle(1);
			SubchainStyleVisitorCoverageProcessingAdapter rsh_SLD = new RasterSymbolizerHelper(
					newCoverage2d, null);

			// visit the RasterSymbolizer
			rsh_SLD.visit(rs_1);
			GridCoverage2D resultCoverage = (GridCoverage2D) rsh_SLD.execute();
			mediaImage = resultCoverage.getRenderedImage();
		}
		else
		{
			mediaImage = sourceimage;
		}
//		Raster mirst = mediaImage.getData();
////		Point pt = new Point(mirst.getSampleModelTranslateX(),mirst.getSampleModelTranslateY());
//		WritableRaster mediaraster = Raster.createWritableRaster(mirst.getSampleModel(),mirst.getDataBuffer(),null);
		WritableRaster mediaraster = (WritableRaster)mediaImage.getData();
		BufferedImage sourcebufferedimage = new BufferedImage(mediaImage.getColorModel(),mediaraster, false, null);
		Image scaleImage = sourcebufferedimage.getScaledInstance(ImageWidth,ImageHeight, BufferedImage.TYPE_3BYTE_BGR);
		BufferedImage resultBufferedImage = new BufferedImage(ImageWidth, ImageHeight, BufferedImage.TYPE_3BYTE_BGR);
		Graphics2D g = resultBufferedImage.createGraphics();
		g.drawImage(scaleImage, 0, 0, null);
		g.dispose();
		
		return resultBufferedImage;
	}
	
	private GridCoverage2D creategeotifffromrenderimage(RenderedImage sourceimage,Envelope env,final GridSampleDimension[] bands)
	{
		GridCoverageFactory gf = new GridCoverageFactory();
	    GridCoverage2D newCov = gf.create("newtiff", sourceimage, env, bands,null,null);
		return newCov;
	}
	

	 private RasterSymbolizer createGreyscaleStyle(int band) {
		 StyleFactory sf = CommonFactoryFinder.getStyleFactory();
		FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
	        ContrastEnhancement ce = sf.contrastEnhancement(ff.literal(1.0), ContrastMethod.NORMALIZE);
	        SelectedChannelType sct = sf.createSelectedChannelType(String.valueOf(band), ce);

	        RasterSymbolizer sym = sf.getDefaultRasterSymbolizer();
	        ChannelSelection sel = sf.channelSelection(sct);
	        sym.setChannelSelection(sel);

	        return sym;
	    }

}
